package com.swagger.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
/**
 * @Author Janson
 * @Date 2022/3/5 15:27
 * @Version 1.0
 */
@Configuration
@EnableSwagger2 // 开启swagger ,也可以把该注解放在 SwaggerApplication 启动类上方
//如果放在此处出现无法访问，可以尝试放在启动类上方
public class SwaggerConfig {
    //docket1 和 docket 两个类的对象，主要 是对api进行分组，各个分组负责自己的api
    //@Bean
    //public Docket docket1(){
    //    return new Docket(DocumentationType.SWAGGER_2).groupName("小葵花");
    //}
    @Bean
    public Docket docket(Environment environment){
        //获取 环境变量
        //设置要显示的swagger 环境
        Profiles profiles = Profiles.of("dev");
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否启动swagger
                .enable(flag)
                .groupName("大太阳")
                .select()
                //RequestHandlerSelectors.basePackage("com.swagger.swagger.controller") 指定扫描的包
                //RequestHandlerSelectors.any 表示全部扫描，一般都是指定扫描包
                //RequestHandlerSelectors.none 表示都不扫描
                .apis(RequestHandlerSelectors.basePackage("com.swagger.swagger.controller"))
                //路径过滤
                //.paths(PathSelectors.ant("/swagger/**"))
                .build();
    }
    private ApiInfo apiInfo(){
        Contact DEFAULT_CONTACT = new Contact("Janson", "http://www.baidu.com", "12242qq.com");
        return new ApiInfo("dongGod",
                "我要好好写代码",
                "1.0",
                "http://www.baidu.com",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
